import kpsImageCutter from '@/components/ksp-image-cutter/ksp-image-cutter.vue'
export default {
  data() {
    return {
      pageLoading: false,
      title: '移除成功',
      isRemove: false, //移除模态框
      roleId: 0, //角色id
      isBack: false,
      avatarUrl: '', //保存头像地址
      userInfo: '', //用户信息
      //管理员
      show: false,
      admin: [{
        name: '员工',
        id: 0,
      }, ],
      //超级管理员
      showTow: false,
      Role: [{
        name: '管理员',
        id: 1,
      },
      {
        name: '员工',
        id: 0,
      },
      ],
      RoleId: '', //权限id
      JobName: '', //职务
      companyId: '',
      uid: '',
      roleName: '',
      roleNamess: '', //个人信息权限
      isName: '', //登录返回的权限
      tid: '', //登录返回的的用户id

      meFlag: false, // 判断是否是我的页面跳转过来的开关

      // 图片裁剪
      url: '',
      path: '',
      selectLog: true, //选择图片为true
      maskshow: false
    }
  },
  // computed: {
  //  role(){
  //    return this.$store.state.user.userInfo.role
  //  }
  // },
  components: {
    kpsImageCutter
  },
  created() {
    if (!this.meFlag) {
      // this.zhs()
      // this.isName = this.$store.state.userInfoDetail.user.roleName
      this.tid = this.$store.state.user.userInfo.tid

      // 角色类型(0、员工 1、管理员 2、超级管理员)
    }
  },
  onShow() {
    this.user()

    this.getCompanyUserInfo()
  },

  onLoad(op) {
    let ops = JSON.parse(op.uid)
    this.uid = ops.uid
    this.companyId = this.$store.state.companyId
    console.log(this.$store.state.user.userInfo.mobile)
  },
  methods: {
    async user() {
      try {
        let data = {
          Uid: uni.getStorageSync('Ztc_B_USER_INFO').tid,
        }
        const res = await this.$api.company.getCompanyUserInfo(data)

        this.isName = res.data.roleName
        this.$store.commit('setUserRole', res.data.role)
        this.$store.commit('user/setUserRole', res.data.role)
        let userInfo = uni.getStorageSync('Ztc_B_USER_INFO')
        let userInfoDetail = uni.getStorageSync('userInfoDetail')
        userInfo.role = res.data.role
        userInfoDetail.user.roleName = res.data.roleName
        uni.setStorageSync('Ztc_B_USER_INFO', userInfo)
        uni.setStorageSync('userInfoDetail', userInfoDetail)
        this.goUserInfo()
        // console.log(userInfoDetail.store)
      } catch (e) {
        console.log(e)
      }

    },
    // 个人信息
    goUserInfo() {
      let option = JSON.stringify({
        uid: uni.getStorageSync('Ztc_B_USER_INFO').tid,
      })
      let uid = option.uid
    },
    //修改手机号
    editPhone() {
      uni.navigateTo({
        url: '../changeTel/changeTel',
      })
    },
    //权限转换
    zhs() {
      let role = this.$store.state.user.userInfo.role

      if (role === 0) {
        this.isName = '员工'

      } else if (role === 1) {

        this.isName = '管理员'
      } else if (role === 2) {

        this.isName = '超级管理员'
      }
    },
    //修改职务失去焦点出发
    blurJob(e) {

      this.JobName = e.detail.value
    },
    //打开权限模态框
    onRole(e, id) {
      //打开超级权限模态框
      this.RoleId = id
      this.userInfo.roleName = e
      if (this.userInfo.roleName === e) {
        this.show = false
      }

    },
    bigOnRole(e, id) {
      this.RoleId = id
      this.userInfo.roleName = e
      if (this.userInfo.roleName === e) {
        this.showTow = false
      }

    },
    //个人信息列表
    async getCompanyUserInfo() {
      try {
        // uni.showLoading()

        let data = {
          Uid: this.uid,
        }
        const res = await this.$api.company.getCompanyUserInfo(data)

        this.userInfo = res.data
        this.$nextTick(() => {
          this.pageLoading = true
        })
        this.JobName = res.data.jobName
        this.roleId = res.data.role
        this.roleNamess = res.data.roleName

      } catch (e) {
        console.log(e)
      }
      // uni.hideLoading()
    },

    //保存头像
    async updateHeadimg() {
      try {
        let data = {
          Uid: this.uid, //用户id
          HeadImg: this.userInfo.headImg, //图片地址
          JobName: this.JobName, //职务
          Role: this.RoleId, //角色类型0员工1管理员
        }
        const res = await this.$api.company.updateCompanyUserInfo(data)
        this.$refs.uToast.show({
          title: '保存成功',
          type: 'success',
        })

        // console.log(res, '保存成功')
      } catch (e) {
        console.log(e)
      }
    },

    //保存信息
    async updateCompanyUserInfo(type) {
      console.log(type)
      if(type===1){
        // uni.showLoading({
        //   title: '上传中'
        // })
      }else{
        uni.showLoading({
				  title: '正在保存'
        })
        this.maskshow=true
      }

      try {
        let data = {
          Uid: this.uid, //用户id
          HeadImg: this.userInfo.headImg, //图片地址
          JobName: this.JobName, //职务
          Role: this.RoleId, //角色类型0员工1管理员
        }
        const res = await this.$api.company.updateCompanyUserInfo(data)
        if (type !== 1) {
          this.$refs.uToast.show({
            title: '保存成功',
            type: 'success',
            back: true
          })
          this.maskshow=false
          // setTimeout(() => {
          //   uni.navigateBack(1)
          // },2000)
        } else {
          let userInfoDetail = uni.getStorageSync('userInfoDetail')
          userInfoDetail.user.headImg = this.userInfo.headImg
          uni.setStorageSync('userInfoDetail', userInfoDetail)
          uni.hideLoading()
        }

        // console.log(res, '保存成功')
      } catch (e) {
        console.log(e)
        if (type === 1) {
          uni.hideLoading()
        }
      }
    },
    //移除
    async removeUser() {
      uni.showLoading({title: '操作中'})
      this.isRemove=false
      this.maskshow=true
      if (this.uid === this.tid) {
        this.title = '离职成功'
      }
      try {
        let data = {
          Uid: this.uid, //用户id
          pid: this.$store.state.userInfoDetail.store.storeId
        }
        const res = await this.$api.company.removeUser(data)
        if (res.code === 200) {
          if (this.uid === this.tid) {
            this.$refs.uToast.show({
              title: this.title,
              type: 'success',
            })
            this.$u.debounce(this.logout, 1000)
          } else {
            this.$refs.uToast.show({
              title: this.title,
              type: 'success',
              back: true
            })
          }

        }
        this.maskshow=false
        uni.hideLoading()
      } catch (e) {
				 uni.hideLoading()
				 this.maskshow=false
        console.log(e)
      }

    },

    //退出登录
    logout() {
      // #ifdef APP-PLUS
      // 更新用户推送设备
      this.$api.user.AddOrEditUniPush({
        type: 'logout',
        cid: plus.push.getClientInfo().clientid,
        platform: uni.getSystemInfoSync().platform,
        storeId: this.$store.state.userInfoDetail.store.storeId,
        phone: this.$store.state.user.userInfo.mobile
      }).then(res => {
        console.log(res)
      })
      // #endif
      this.$store.commit('resetInfo')
      this.$store.commit('user/resetInfo')
      let promise = this.tim.logout()
      promise
        .then(res => {
          this.$store.commit('news/reset')
        })
        .catch(err => {
          console.log('退出失败')
        })
      uni.removeStorageSync('token')
      uni.removeStorageSync('userInfoDetail')
      uni.removeStorage({
        key: 'Ztc_B_USER_INFO',
        success: function(res) {
          uni.redirectTo({
            url: '/pages_login/login/login',
          })
        },
      })
    },

    //上传头像
    uploadAvatar() {

      uni.chooseImage({
        count: 1,
        sizeType: ['compressed'],
        success: e => {
          this.url = e.tempFilePaths[0]
          this.selectLog = false
          /* this.$api.files
					  .uploadImg({
					    filePath: e.tempFilePaths[0],
					  })
					  .then(res => {
					  })
					  .catch(err => {
					    console.log(err)
					  }) */
        },
        fail: err => {
          // uni.hideLoading()
          this.selectLog = true
        }
      })
    },
    onok(ev) {
      uni.showLoading({
			  title: '上传中'
      })
      this.path = ev.path
      this.$api.files
        .uploadImg({
          filePath: this.path,
        })
        .then(res => {
          // this.avatarUrl = res.baseUrl + res.url
          this.userInfo.headImg = res.baseUrl + res.url

          this.url = ''
          this.updateCompanyUserInfo(1)
        })
        .catch(err => {
          uni.hideLoading()
        })
      this.selectLog = true
    },
    oncancle() {
      // url设置为空，隐藏控件
      this.url = ''
      this.selectLog = true
    },
    //管理员修改权限
    choosePermission() {
      this.show = true
    },
    //超级管理员修改权限
    bigChoosePermission() {
      this.showTow = true
    },
    goZr() {
      // let e= this.companyId
      uni.navigateTo({
        url: '../transferSuper/transferSuper',
      })
    },
    goPage(bagName, url) {
      //bagName 包名 url 页面名称
      uni.navigateTo({
        url: '/' + bagName + '/' + url + '/' + url,
      })
    },
    //邀请
    async doInviteUser() {

      try {
        let data = {
          'CompanyId': this.companyId, //公司id 'f1b343b44a6648c68349d406b62e0c7a'
          'Mobile': this.userInfo.mobile,
          'JobName': this.JobName, //职务
          'IsAdmin': this.isName === '管理员' ? true : false, //是否管理员
          'TrueName': this.userInfo.trueName //姓名
        }
        const res = await this.$api.company.DoInviteUser(data)
        console.log(res)
        this.$u.toast('邀请成功')
        setTimeout(res => {
          uni.navigateBack({
            delta: 1,
          })
        }, 1000)
      } catch (e) {
        console.log(e)
      }
    },
  },
}
